package com.len.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.len.base.BaseController;
import com.len.controller.req.UserReq;
import com.len.entity.UserEntity;
import com.len.service.*;
import com.len.util.JsonUtil;
import com.len.util.ReType;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@RequestMapping(value = "/shop/shopUser")
@Slf4j
@RequiresPermissions("shopUser:showList")
public class ShopUserController extends BaseController {

    @Resource
    private UserService userService;

    @GetMapping(value = "showList")
    public String showList() {
        return "/shop/shopUser/shopUserList";
    }

    @GetMapping(value = "showUpdateShowLimit")
    public String showAddShowLimit(String id, Model model) {
        model.addAttribute("id", id);
        return "/shop/shopUser/updateShowLimit";
    }

    @GetMapping(value = "getList")
    @ResponseBody
    public String getList(UserReq req, String page, String limit) {

        Page<UserEntity> tPage = new Page<>(Integer.parseInt(page), Integer.parseInt(limit));
        tPage = userService.getList(req,tPage);
        ReType reType = new ReType(tPage.getTotal(), tPage.getResult());

        return JSON.toJSONString(reType);
    }

    @PostMapping(value = "updateShowLimit")
    @ResponseBody
    public JsonUtil updateShowLimit(@RequestParam Integer showLimit,@RequestParam String id) throws Exception {

        userService.updateShowLimit(showLimit,id);
        return JsonUtil.success("操作成功");
    }

    @PostMapping(value = "updateVip")
    @ResponseBody
    public JsonUtil updateVip(@RequestParam Integer vip,@RequestParam String id) throws Exception {

        userService.updateVip(vip,id);
        return JsonUtil.success("操作成功");
    }


}
